﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace StarBotDotNet.Tournament1.ZealotBattle
{
    public class AttackParty : UnitParty
    {
        Dictionary<StarBotDotNet.UnitType, int> requiredUnits;
        // The first map is a 9v9 Zealot battle.

        Point center;
        List<Point> battlePosition;


        public AttackParty(ProxyBot bot)
        {
            requiredUnits = new Dictionary<UnitType, int>();
            // the battle is going to be a 12v12 battle.

            Utilities.WriteColorLine(ConsoleColor.Blue, bot.GetCurrentPlayerWorkerType().ToString());

            requiredUnits.Add(bot.GetCurrentPlayerWorkerType(), 9);

            center = new Point(15, 35);

            battlePosition = new List<Point>()
            {
                new Point(center.X+1, center.Y+1),
                new Point(center.X+2, center.Y+2),
                new Point(center.X+3, center.Y+3),
                new Point(center.X+4, center.Y+4),
                new Point(center.X, center.Y),
                new Point(center.X+1, center.Y-1),
                new Point(center.X+2, center.Y-2),
                new Point(center.X+3, center.Y-3),
                new Point(center.X+4, center.Y-4)
            };
        }

        SortedDictionary<int, UnitData> zealotsPlayer = new SortedDictionary<int, UnitData>();
        SortedDictionary<int, UnitData> zealotsOpponent = new SortedDictionary<int, UnitData>();


        internal override void PopulateWithUnits(ProxyBot bot, UnitDictionary currentUnits, List<UnitData> discoveredUnits, List<UnitData> dissapearedUnits, int currentTime)
        {
            // Add the live and discovered targets.
            foreach (UnitData i in discoveredUnits)
            {
                if (i.OwnedByPlayer)
                {
                    if (i.Type.Type == Constants.UnitTypes.Protoss_Zealot && zealotsPlayer.ContainsKey(i.ID) == false)
                    {
                        zealotsPlayer.Add(i.ID, i);
                    }

                }
                else
                {
                    if (i.Type.Type == Constants.UnitTypes.Protoss_Zealot && zealotsOpponent.ContainsKey(i.ID) == false)
                    {
                        zealotsOpponent.Add(i.ID, i);
                    }
                }

            }

            // Remove the dead targets.
            foreach (UnitData i in dissapearedUnits)
            {
                if (i.OwnedByPlayer)
                {
                    if (i.Type.Type == Constants.UnitTypes.Protoss_Zealot && zealotsPlayer.ContainsKey(i.ID) == true)
                    {
                        zealotsPlayer.Remove(i.ID);
                    }

                }
                else
                {
                    if (i.Type.Type == Constants.UnitTypes.Protoss_Zealot && zealotsOpponent.ContainsKey(i.ID) == true)
                    {
                        zealotsOpponent.Remove(i.ID);
                    }
                }

            }

            
            base.PopulateWithUnits(bot, currentUnits, discoveredUnits, dissapearedUnits, currentTime);
        }

        bool movedToPosition = false;
        bool patroling = false;

        public override void ExecuteTactics(ProxyBot bot, UnitDictionary currentUnits, int currentTime)
        {
            // Execute actions
            if (movedToPosition == false)
            {
                int counter = 0;
                foreach (KeyValuePair<int, UnitData> i in zealotsPlayer)
                {
                    bot.attackMove(i.Value.ID, battlePosition[counter].X, battlePosition[counter].Y);
                    counter++;
                }

                movedToPosition = true;
            }

            if (patroling == false && movedToPosition == true)
            {
                bool zealotsIdleInPosition = true;
                foreach (KeyValuePair<int, UnitData> i in zealotsPlayer)
                {
                    if (i.Value.IsIdle == false)
                    {
                        zealotsIdleInPosition = false;
                    }
                }

                if (zealotsIdleInPosition)
                {
                    int counter = 0;
                    foreach (KeyValuePair<int, UnitData> i in zealotsPlayer)
                    {
                        bot.patrol(i.Value.ID, battlePosition[counter].X + 40, battlePosition[counter].Y);
                        counter++;
                    }
                }
            }
            
        }

        public override int Priority
        {
            get { return 0; }
        }

        public override Dictionary<UnitType, int> RequiredUnits
        {
            get { return requiredUnits; }
        }

        protected override bool IsUnused(UnitData data)
        {
            return data.IsIdle;
        }
    }
}
